<?php
	/**
	* Author:rui
	* Email:zer0131@vip.qq.com
	* 后台购物模块
	*/
	class BuyAction extends AdminbaseAction{

        private $goodsobj;
        private $orderobj;
        private $configobj;
		public function _initialize() {
	        parent::_initialize();
            $this->goodsobj = D('Goods');
            $this->orderobj = M('Order');
            $this->configobj = M('Buyconfig');
    	}

    	public function index(){
            $count = $this->goodsobj->count();
            $page = $this->page($count, 12);
            $list = $this->goodsobj->limit($page->firstRow . ',' . $page->listRows)->order('addtime desc')->select();
            $this->assign('list',$list);
            $this->assign("Page", $page->show('Admin'));
            $this->display();
    	}

        //分类列表
    	//public function goodsCategory(){
    		//code
    	//}

        //配置列表
    	public function buyConfig(){
    		$count = $this->configobj->count();
            $page = $this->page($count, 12);
            $list = $this->configobj->limit($page->firstRow . ',' . $page->listRows)->select();
            $this->assign('list',$list);
            $this->assign("Page", $page->show('Admin'));
            $this->display();
    	}

        //增加商品
        public function addGoods(){
            $authkey = upload_key("1,jpg|jpeg|gif|png|bmp,1,,,1");
            $this->assign('authkey', $authkey);
            $this->display();
        }

        //保存新增商品
        public function addGoodsSave(){
            if(!$this->goodsobj->create()){
                $this->error($this->goodsobj->getError());
            }
            //$this->goodsobj->position = $this->_post('posid');
            $result = $this->goodsobj->add();
            if($result){
                $data['goodsurl'] = U('Buy/Index/detail',array('id'=>$result));
                $this->goodsobj->where(array('id'=>$result))->save($data);
                $this->success('商品添加成功');
            }
            else{
                $this->error('商品添加失败');
            }
        }

        //编辑商品
        public function editGoods(){
            $id = $this->_get('id');
            if(!$id){
                $this->error('参数有误');
            }
            $map['id'] = $id;
            $data = $this->goodsobj->where($map)->find();
            if($data){
                $authkey = upload_key("1,jpg|jpeg|gif|png|bmp,1,,,1");
                $this->assign('authkey', $authkey);
                $this->assign('data', $data);
            }
            else{
                $this->error('该项不存在');
            }
            $this->display();
        }

        //更新商品
        public function updateGoods(){
            if(!$this->goodsobj->create()){
                $this->error($this->goodsobj->getError());
            }
            //$this->goodsobj->position = $this->_post('posid');
            $result = $this->goodsobj->save();
            if($result){
                $this->success('更新商品成功');
            }
            else{
                $this->error('更新商品失败');
            }
        }

        //删除商品
        public function delGoods(){
            $id = $this->_get('id');
            if(!$id){
                $this->error('参数有误');
            }
            $where['id'] = $id;
            $result = $this->goodsobj->where($where)->delete();
            if($result){
                $this->success('商品删除成功');
            }
            else{
                $this->error('商品删除失败');
            }
        }

        //编辑配置项
        public function editCofig(){
            $id = $this->_get('id');
            if(!$id){
                $this->error('参数错误');
            }
            $data = $this->configobj->where(array('id'=>$id))->find();
            if($data){
                $this->assign('data', $data);
            }
            else{
                $this->erroe('该项不存在');
            }
            $this->display();
        }

        //更新配置项
        public function updateConfig(){
            if(!$this->configobj->create()){
                $this->error($this->configobj->getError());
            }
            $result = $this->configobj->save();
            if($result){
                $this->success('更新账号信息成功');
            }
            else{
                $this->error('更新失败');
            }
        }

        //订单列表
        public function orderList(){
            $count = $this->orderobj->count();
            $page = $this->page($count, 12);
            $list = $this->orderobj->limit($page->firstRow . ',' . $page->listRows)->order('addtime desc')->select();
            foreach ($list as $key => $val) {
                $map['userid'] = $val['uid'];
                $user = M('Member')->field('userid,username')->where($map)->find();
                $list[$key]['username'] = $user['username'];
                $list[$key]['ispayname'] = getOrderStatus($val['ispay']);
            }
            $this->assign('list',$list);
            $this->assign("Page", $page->show('Admin'));
            $this->display();
        }

        //订单详情
        public function orderDetails(){
            $id = $this->_get('id');
            if(!$id){
                $this->error('参数有误');
            }
            $map['id'] = $id;
            $data = $this->orderobj->where($map)->find();
            if($data){
                $umap['userid'] = $data['uid'];
                $user = M('Member')->field('username')->where($umap)->find();
                $data['username'] = $user['username'];

                $gmap['id'] = $data['goodsid'];
                $goods = $this->goodsobj->field('title,nowprice,oldprice,starttime,endtime,category')->where($gmap)->find();
                $data['title'] = $goods['title'];
                $data['nowprice'] = $goods['nowprice'];
                $data['oldprice'] = $goods['oldprice'];
                $data['starttime'] = $goods['starttime'];
                $data['endtime'] = $goods['endtime'];
                $data['ispayname'] = getOrderStatus($data['ispay']);
                if($goods['category'] == '1'){
                    $data['category'] = '新房';
                }
                else if($goods['category'] == '2'){
                    $data['category'] = '二手房';
                }
                else{
                    $data['category'] = '未知';
                }
                $this->assign('data', $data);
            }
            else{
                $this->error('该项不存在');
            }
            $this->display();
        }

        //删除订单
        public function delOrder(){
            $id = $this->_get('id');
            if(!$id){
                $this->error('参数有误');
            }
            $where['id'] = $id;
            $result = $this->orderobj->where($where)->delete();
            if($result){
                $this->success('订单删除成功');
            }
            else{
                $this->error('订单删除失败');
            }
        }
	}
?>